﻿package net.wangit.util;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import net.wangit.MapList;


public class Checker {

	public Checker() {
	}

	public static final boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isEmpty(String array[]) {
		return array == null || array.length == 0;
	}

	public static boolean isEmpty(String array[][]) {
		return array == null || array.length == 0;
	}

	@SuppressWarnings("unchecked")
	public static boolean isEmpty(List list) {
		return list == null || list.size() < 1;
	}

	@SuppressWarnings("unchecked")
	public static boolean isEmpty(Map map) {
		return map == null || map.size() < 1;
	}

	public static boolean isEmpty(MapList data) {
		return data == null || data.size() < 1;
	}

	public static boolean isInteger(String str) {
		try {
			Integer.parseInt(str);
		} catch (Exception ex) {
			return false;
		}
		return true;
	}

	public static boolean isPositiveInteger(String str) {
		try {
			int i = Integer.parseInt(str);
			if (i > 0)
				return true;
			return false;
		} catch (Exception ex) {
			return false;
		}
	}

	public static boolean isNonnegativeInteger(String str) {
		try {
			int i = Integer.parseInt(str);
			if (i >= 0)
				return true;
			return false;
		} catch (Exception ex) {
			return false;
		}
	}

	public static boolean isLong(String str) {
		try {
			Long.parseLong(str);
		} catch (Exception ex) {
			return false;
		}
		return true;
	}

	public static boolean isPositiveLong(String str) {
		try {
			long l = Long.parseLong(str);
			if (l > 0L)
				return true;
			return false;
		} catch (Exception ex) {
			return false;
		}
	}

	public static boolean isNonnegativeLong(String str) {
		try {
			long l = Long.parseLong(str);
			if (l >= 0L)
				return true;
			return false;
		} catch (Exception ex) {
			return false;
		}
	}

	public static boolean isDecimal(String str) {
		try {
			Double.parseDouble(str);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public static boolean isPositiveDecimal(String str) {
		try {
			double d = Double.parseDouble(str);
			if (d > 0.0D)
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isNonnegativeDecimal(String str) {
		try {
			double d = Double.parseDouble(str);
			if (d >= 0.0D)
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isDate(String str) {
		try {
			String formatStr;
			java.util.Date date = DateUtil.getDateFormat().parse(str);
			formatStr = DateUtil.getDateFormat().format(date);
			return formatStr.equals(str);
		} catch (Exception ex) {
			return false;
		}
	}

	public static boolean isTime(String str) {
		try {
			String formatStr;
			java.util.Date t = DateUtil.getTimeFormat().parse(str);
			formatStr = DateUtil.getTimeFormat().format(t);
			return formatStr.equals(str);
		} catch (Exception e) {
			return false;
		}
	}

	public static boolean isTimestamp(String str) {
		if (str == null || str.length() < 19)
			return false;
		if (str.length() == 19 && !str.endsWith("."))
			str = (new StringBuilder(String.valueOf(str))).append(".")
					.toString();
		for (; str.length() < 23; str = (new StringBuilder(String.valueOf(str)))
				.append("0").toString())
			;
		try {
			String formatStr;
			java.util.Date date = DateUtil.getTimestampFormat().parse(str);
			formatStr = DateUtil.getTimestampFormat().format(date);
			return formatStr.equals(str);
		} catch (ParseException ex) {
			return false;
		}
	}
}
